<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head th:replace="common/head::head">
</head>

<body>
<style type="text/css">
    .randCode {
        padding: 0;
        min-height: 36px;
        height: 38px;
    }
</style>
<div class="layui-carousel auto" id="div-carousel" style=" position: fixed; z-index: 0;">
    <div carousel-item>
        <div><img th:src="@{${session.jpomProxyPath}+'/static/backgrounds/1.jpg?v='+${session.staticCacheTime}}"
                  class="auto"></div>
        <div><img th:src="@{${session.jpomProxyPath}+'/static/backgrounds/2.jpg?v='+${session.staticCacheTime}}"
                  class="auto"></div>
        <div><img th:src="@{${session.jpomProxyPath}+'/static/backgrounds/3.jpg?v='+${session.staticCacheTime}}"
                  class="auto"></div>
    </div>
</div>
<div class="layui-container" style="margin-top: 150px;">
    <div class="layui-row">
        <div class="layui-col-md4 layui-col-md-offset4">
            <form class="layui-form" method="post" action="userLogin">
                <div class="layui-form-item" style="color: #FFF; font-size: 24px;text-align: center;">
                    <span>Jpom项目管理系统</span>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="margin-left: 0px;">
                        <input type="text" required lay-verify="required" class="layui-input" placeholder="请输入用户名"
                               name="userName">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="margin-left: 0px;">
                        <input type="password" required lay-verify="required" class="layui-input" placeholder="请输入密码"
                               name="userPwd">
                    </div>
                </div>
                <div class="layui-form-item" id="randCodeDiv" style="display: none;">
                    <div class="layui-input-inline" style="margin-left: 0px;">
                        <input type="text" name="code" placeholder="请输入4位验证码" autocomplete="off"
                               class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux" onclick="showCode();"
                         style="padding: 0 !important; margin: 0;">
                        <img src="" id="codeImg" class="randCode">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="margin-left: 0;" id="loginBtn">
                        <button class="layui-btn layui-btn-danger" lay-submit lay-filter="from_login"
                                style="width: 100%">登录
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div th:replace="common/sha1::sha1"></div>
</body>
<script type="text/javascript">
    function loadSuccess() {
        layui.use(['carousel'], function () {
            var carousel = layui.carousel;

            //   判断是否在iframe中, 如果是，刷新父页面
            if (window.self !== window.top) {
                parent.window.location.replace(window.location.href);
            }
            //
            const user = layui.data('user');
            if (user && user.userName) {
                $("input[name='userName']").val(user.userName);
            }
            carousel.render({
                elem: '#div-carousel',
                width: '100%',
                height: '100%',
                anim: 'fade',
                arrow: 'none',
                indicator: 'none'
            });
            $("form").submit(function (d) {
                const paramArray = $("form").serializeArray();
                const jsonObj = {};
                $(paramArray).each(function () {
                    jsonObj[this.name] = this.value;
                });
                doLogin(jsonObj);
                return false;
            });
            form.on('submit(from_login)', function (dataForm) {
                doLogin(dataForm.field);
                return false;
            });

            function doLogin(dataForm) {
                if (!checkPwd(dataForm.userPwd)) {
                    return false;
                }
                var sendData = formatPwd(dataForm, "userPwd");
                loadingAjax({
                    url: './userLogin',
                    data: sendData,
                    success: function (data) {
                        if (200 === data.code) {
                            // 记住用户名
                            layui.data('user', {
                                key: "userName",
                                value: sendData.userName
                            });
                            layui.data('user', {
                                key: "loginOut",
                                remove: true
                            });
                            var url = getQueryString("url");
                            if (url && url.indexOf("index") > -1) {
                                window.location.href = decodeURIComponent(url);
                            } else {
                                window.location.href = './index.html';
                            }
                        } else {
                            // 显示验证码
                            if (600 === data.code) {
                                showCode();
                            }
                            layer.msg(data.msg);
                        }
                    }
                });
            }

            if ([[${showCode}]])
                showCode();
        });
    }

    function showCode() {
        $("#codeImg").attr("src", "./randCode.png?time=" + new Date().getTime());
        $("#randCodeDiv").show();
    }
</script>

</html>